import {
    containerEl,
    bottomEl,
    cardWidth,
    openArray,
    topOpenArr,
    openCard,
    cardHeight
} from "./varible.js";
import {
    addTableCard,
    addBottomCard,
    addCard,
} from './addTable.js'
import {
    changeLocation
} from './method.js'

// 使用了一个二维数组，第一维为纸牌的下标，子数组0记录当前节点的left值，
//1记录top值，预留2，3，4，5记录连接节点，6记录父节点个数，7-10记录子节点index,11,12存储aov_list的下标
let preLast = 0;


function initTableCard(aov_list, array, jsonArray, fIndex) {
    let leftNodeArr = [];
    let levelSize = aov_list.length;
    let headIndex = parseInt(aov_list[0][0].index);
    let tailIndex = 0;
    for (let i = 0; i < aov_list.length; i++) {
        let firstIndex = aov_list[i][0].index;
        leftNodeArr.push(parseInt(firstIndex));
        for (let j = 0; j < aov_list[i].length; j++) {

            //当前节点上的数字
            let number = aov_list[i][j].card.number;
            //当前节点的下标Index
            let index = aov_list[i][j].index;
            jsonArray[index] = aov_list[i][j];
            tailIndex = index;
            //存储该节点的子节点
            let childArr = aov_list[i][j].children;
            for (let i = 0; i < childArr.length; i++) {
                array[index][7 + i] = childArr[i];
                array[childArr[i]][6]++;
                //填充子节点数组的父节点位置
                for (let k = 2; k <= 5; k++) {
                    if (array[childArr[i]][k] == -1) {
                        array[childArr[i]][k] = index;
                        break;
                    }
                }
            }

            //没有父节点的元素，即掀开的牌 
            if (array[index][0] == 0 && array[index][1] == 0) {
                if (index == 0) {
                    array[index][0] = (index - firstIndex) * cardWidth / 2 * 3 + cardWidth;
                    topOpenArr.push(number);
                } else {
                    array[index][0] = array[index - 1][0] + cardWidth / 2 * 3;
                    topOpenArr.push(number);
                }
            }
            //未设置其top值时在这里设置
            if (array[index][1] == 0) {
                // array[index][1] = (aov_list.length - 1 - i) * 95 + 10;
                array[index][1] = (aov_list.length - 1 - i) * cardHeight / 4 * 3 + 5 + 10;
            }

            while (((index == firstIndex) && (array[index][0] < (preLast + cardWidth)))) {
                array[index][0] += cardWidth;
                changeLocation(array, index, cardWidth);
            }
            while ((index != firstIndex) && ((array[index][0] - array[index - 1][0]) <= cardWidth) && (array[index][1] == array[index - 1][1])) {
                //if如果盖住了前一张牌挪动cardWidth的宽度,else if如果与前一张牌相邻就挪动cardWidth/2的宽度
                if ((array[index][0] - array[index - 1][0]) < cardWidth) {
                    array[index][0] += cardWidth;
                    changeLocation(array, index, cardWidth);
                } else if ((array[index][0] - array[index - 1][0]) == cardWidth) {
                    array[index][0] += cardWidth / 2;
                    changeLocation(array, index, cardWidth / 2);
                }
                //节点被非父节点（父节点的前一个节点，但不是该节点的父节点）压住
                // if ((array[index][2] != -1) && (array[index][2] != (array[index][2] - 1)) && ((array[index][0] - array[array[index][2] - 1][0]) < cardWidth) && (array[index][0] > array[array[index][2] - 1][0])) {
                //     array[index][0] += cardWidth / 2;
                //     changeLocation(array, index, cardWidth / 2);
                // }
                //节点被非父节点（父节点的后一个节点，但不是该节点的父节点）压住
                // if ((array[index][2] != -1) && (array[index][3] == -1) && ((array[array[index][2] + 1][0] - array[index][0]) < cardWidth) && (array[array[index][2] + 1][0] > array[index][0])) {
                //     console.log(index, array[index][0], array[array[index][2] + 1][0], array[index][2] + 1);
                //     array[array[index][2] + 1][0] += cardWidth / 2;
                //     changeLocation(array, array[index][2] + 1, cardWidth / 2);
                // }
                //console.log(index + " " + array[index][0] + " " + array[index - 1][0]);
            }

            //有一个子节点的元素,需判断一下子节点的父节点个数
            if (childArr.length == 1) {
                let childIndex = childArr[0];
                //只有一个父节点
                // if (array[childIndex][2] == -1) {
                if (array[childIndex][6] == 1) {
                    array[childIndex][0] = array[index][0];
                    array[childIndex][2] = index;
                } else if (array[childIndex][6] == 2) {
                    //有两个父节点
                    array[childIndex][0] = array[array[childIndex][2]][0] + cardWidth / 4 * 3;
                    array[childIndex][3] = index;
                }

            } else if (childArr.length == 2) {
                array[childArr[0]][0] = array[index][0] - cardWidth / 4 * 3;
                array[childArr[1]][0] = array[index][0] + cardWidth / 4 * 3;
            } else if (childArr.length == 3) {
                array[childArr[0]][0] = array[index][0] - cardWidth / 4 * 3;
                array[childArr[1]][0] = array[index][0] + cardWidth / 4 * 3;
                array[childArr[2]][0] = array[index][0];
                array[childArr[2]][1] = array[index][1] + cardWidth;
            } else if (childArr.length == 4) {
                array[childArr[0]][0] = array[index][0] - cardWidth / 4 * 3;
                array[childArr[1]][0] = array[index][0] + cardWidth / 4 * 3;
                array[childArr[0]][1] = array[childArr[1]][1] = array[index][1] - cardWidth;
                array[childArr[2]][0] = array[index][0] - cardWidth / 4 * 3;
                array[childArr[3]][0] = array[index][0] + cardWidth / 4 * 3;
                array[childArr[2]][1] = array[childArr[3]][1] = array[index][1] + cardWidth;
            }
            array[index][11] = i;
            array[index][12] = j;
            array[index][13] = fIndex;
        }
    }

    for (let k = headIndex; k <= tailIndex; k++) {
        if (array[k][11] >= 0) {
            if (array[k][0] > preLast) {
                preLast = array[k][0];
            }
            // console.log(array[k][11], array[k][12], aov_list);
            if (typeof aov_list[array[k][11]][array[k][12]] === 'undefined') {} else {
                let divEl = addTableCard(aov_list[array[k][11]][array[k][12]], array, levelSize);
                containerEl.appendChild(divEl);

            }

        }
    }

}


function initBottomCard(bottom_list, array) {
    for (let i = bottom_list.length - 1; i >= 0; i--) {
        let divEl = addCard(bottom_list[i]);
        divEl.className = "picBtm";
        divEl.style.zIndex = bottom_list[bottom_list.length - 1 - i].card.index;
        //初始底部非掀开牌的位置
        if (i != 0) {
            divEl.style.backgroundPosition = "0px " + cardHeight * 6 + "px";
            divEl.style.left = ((bottom_list.length - 1 - i) * 2 + 40) + 'px';
            divEl.style.top = 20 + 'px';
        } else {
            //初始底部掀开的一张牌
            divEl.style.backgroundPosition = (bottom_list[i].card.number - 1) * -1 * cardWidth + "px 0px";
            divEl.style.left = ((bottom_list.length - 1 - i) * 2 + cardWidth + 80) + 'px';
            divEl.style.top = 20 + 'px';
            // divEl.style.zIndex = 0;
            divEl.className = "openPic";
            // openArray.push(parseInt(bottom_list[i].card.number));
            openArray[0] = (parseInt(bottom_list[i].card.number));
            // openCard = parseInt(bottom_list[i].card.number);
        }
        bottomEl.appendChild(divEl);
    }
    let btmNum = document.createElement("div");
    btmNum.className = "bCardLength";
    btmNum.innerHTML = parseInt(bottom_list.length) - 1;
    bottomEl.appendChild(btmNum);
}
export {
    initTableCard,
    initBottomCard
}